/*!
 * jQuery Cookie Plugin v1.4.1
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2013 Klaus Hartl
 * Released under the MIT license
 */
(function (factory) {
	if (typeof define === 'function' && define.amd) {
		// AMD
		define(['jquery'], factory);
	} else if (typeof exports === 'object') {
		// CommonJS
		factory(require('jquery'));
	} else {
		// Browser globals
		factory(jQuery);
	}
}(function ($) {

	var pluses = /\+/g;

	function encode(s) {
		return config.raw ? s : encodeURIComponent(s);
	}

	function decode(s) {
		return config.raw ? s : decodeURIComponent(s);
	}

	function stringifyCookieValue(value) {
		return encode(config.json ? JSON.stringify(value) : String(value));
	}

	function parseCookieValue(s) {
		if (s.indexOf('"') === 0) {
			// This is a quoted cookie as according to RFC2068, unescape...
			s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
		}

		try {
			// Replace server-side written pluses with spaces.
			// If we can't decode the cookie, ignore it, it's unusable.
			// If we can't parse the cookie, ignore it, it's unusable.
			s = decodeURIComponent(s.replace(pluses, ' '));
			return config.json ? JSON.parse(s) : s;
		} catch(e) {}
	}

	function read(s, converter) {
		var value = config.raw ? s : parseCookieValue(s);
		return $.isFunction(converter) ? converter(value) : value;
	}

	var config = $.cookie = function (key, value, options) {

		// Write

		if (value !== undefined && !$.isFunction(value)) {
			options = $.extend({}, config.defaults, options);

			if (typeof options.expires === 'number') {
				var days = options.expires, t = options.expires = new Date();
				t.setTime(+t + days * 864e+5);
			}

			return (document.cookie = [
				encode(key), '=', stringifyCookieValue(value),
				options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
				options.path    ? '; path=' + options.path : '',
				options.domain  ? '; domain=' + options.domain : '',
				options.secure  ? '; secure' : ''
			].join(''));
		}

		// Read

		var result = key ? undefined : {};

		// To prevent the for loop in the first place assign an empty array
		// in case there are no cookies at all. Also prevents odd result when
		// calling $.cookie().
		var cookies = document.cookie ? document.cookie.split('; ') : [];

		for (var i = 0, l = cookies.length; i < l; i++) {
			var parts = cookies[i].split('=');
			var name = decode(parts.shift());
			var cookie = parts.join('=');

			if (key && key === name) {
				// If second argument (value) is a function it's a converter...
				result = read(cookie, value);
				break;
			}

			// Prevent storing a cookie that we couldn't decode.
			if (!key && (cookie = read(cookie)) !== undefined) {
				result[name] = cookie;
			}
		}

		return result;
	};

	config.defaults = {};

	$.removeCookie = function (key, options) {
		if ($.cookie(key) === undefined) {
			return false;
		}

		// Must not alter options, thus extending a fresh object...
		$.cookie(key, '', $.extend({}, options, { expires: -1 }));
		return !$.cookie(key);
	};

}));

//购物车
var Cart={
	init: function(){
		var aa = {};
		aa.count = 0;
		aa.total=0;
		aa.items=new Array();
		return aa;
	}
}
//购物车集合对象
var  CartItem={
	init: function(){
		var aa = {};
		//购物车列表中的id 不是商品id  这个是商品id 加规格的结合体
		aa.id = 0;
		//商品id
		aa.goodid=0;
		aa.name="";
		aa.count=0;
		aa.price=0;
		aa.img="";
		//规格详情
		aa.xiangqing="";
		return aa;
	}
};
//购物车操作
var CartHelper ={
	init:function(){
		var aa={};
		aa.cookiename =  "yxhCart";
		aa.Cart=null;
		aa.getCart=function(){
			if(aa.Cart==null){
				aa.Cart=aa.Read(); 
			}
			return aa.Cart;
		};
		aa.Clear=function(){
			aa.Cart = Cart.init();
			aa.Save();
			return aa.Read();
			
		};
		aa.add=function (id, goodid,name, count, price,img,xiangqing) {
			var cart = aa.getCart();
			var index = aa.Find(id);
			//如果id已存在，覆盖数量
			if (index > -1) {
				
				cart.items[index].count+=count;
				
				
			} else {
				var item = CartItem.init();
				item.id = id;
				item.goodid=goodid;
				item.name = name;
				item.count = count;
				item.price = parseInt(price*100);
				item.img=img;
				item.xiangqing=xiangqing;
				cart.items.push(item);
			}
			cart.count+=count;
			cart.total += parseInt(price*100*count);
			aa.Save();
			return cart;
		};
		aa.addOne=function(id){
			var cart = aa.getCart();
			var index = aa.Find(id);
			if(index==-1){
				return false;
			}
			cart.items[index].count+=1;
			cart.count+=1;
			cart.total += cart.items[index].price;
			aa.Save();
			return true;
			
			
		};
		aa.DelOne=function (id) {
			var cart = aa.getCart();
			var index = aa.Find(id);
			if (index > -1) {
				var item = cart.items[index];
				cart.count--;
				cart.total = cart.total - item.price;
				if(item.count==1){
					cart.items.splice(index, 1);
				}else{
					cart.items[index].count--;
				}
				aa.Save();
			}
			return aa.getCart();
			
		};
		aa.Del=function (id) {
			var cart = aa.getCart();
			var index = aa.Find(id);
			console.log(index);
			if (index > -1) {
				var item = cart.items[index];
				cart.count=cart.count-item.count;
				cart.total = cart.total - item.price*item.count;
				cart.items.splice(index, 1);
				aa.Save();
			}
			return aa.getCart();
		};
		
		aa.Find = function (id) {
			var cart = aa.getCart();
			var index = -1;
			for (var i = 0; i < cart.items.length; i++) {
				if (cart.items[i].id == id) {
					index = i;
				}
			}
			return index;
		};
  
  
		aa.Save=function () {
			var source =  JSON.stringify(aa.getCart());
			
			
			console.log(source);
			$.cookie(this.cookiename, source);
			aa.Cart=null;
		};
		aa.Read=function () {
			//读取COOKIE中的集合
			var source = $.cookie(this.cookiename);
			if(source==''||source==undefined){
				var cart = Cart.init();
				return cart;
			}
			
			var cart = JSON.parse(source);
			cart.count=0;
			cart.total=0;
			for (var i = 0; i < cart.items.length; i++) {
				cart.count+=cart.items[i].count;
				cart.total+=cart.items[i].count*cart.items[i].price;
			}
			cart.count=parseInt(cart.count);
			cart.total=parseInt(cart.total);
			console.log(cart);
			return cart;
		};
		
		aa.Finditem=function(id){
			var cart = aa.getCart();
			var index = -1;
			for (var i = 0; i < cart.items.length; i++) {
				if (cart.items[i].id == id) {
					index = i;
				}
			}
			if(index>-1){
				return cart.items[index];
			}
			return null;
			
		};
		return aa;
	}
};


var cartHelper=CartHelper.init();
var shangpinhistorycookiename="shangpinhistory";
var shangpinhistory= $.cookie(shangpinhistorycookiename);
if(shangpinhistory==''||shangpinhistory==undefined){
	
	shangpinhistory=[];
}else{
	shangpinhistory=JSON.parse(shangpinhistory);
}

function addshangpinhistory(shangpinid){
	var ishave=false;
	$.each(shangpinhistory,function(index,item){
		if(item==shangpinid){
			ishave=true;
		}
	
	});
	if(!ishave){
		shangpinhistory.push(shangpinid);
		$.cookie(shangpinhistorycookiename, JSON.stringify(shangpinhistory));
		if(shangpinhistory.length>15){
			shangpinhistory.splice(0,1);
		}
	}
}

var payshangpingidscookiename="payshangpingidscookiename";
var payshangpingids= $.cookie(payshangpingidscookiename);
if(payshangpingids==''||payshangpingids==undefined){
	
	payshangpingids=[];
}else{
	payshangpingids=JSON.parse(payshangpingids);
}
function getpayshangpingids(){
	return payshangpingids;
}
 

function savepayshangpingids(aa){
	$.cookie(payshangpingidscookiename, JSON.stringify(aa));
}



var addresslastname="addresslast";
var address_lastid=$.cookie(addresslastname);
if(address_lastid==''||address_lastid==undefined){
	address_lastid=-1;
}else{
	address_lastid=parseInt(address_lastid);
}
function savemylast(addid){
	address_lastid=addid;
	$.cookie(addresslastname, addid);
	console.log(address_lastid);
	
}
console.log(address_lastid);

